$(function () {
    $('form').parsley();
    dynamicBinding();
    getAllForeignBase();
});

//遍历所有foreignBase输出到表格
function getAllForeignBase() {
    let deleteStr = "";
    let updateStr = "";
    let columns = [
        {
            title: "序号", data: null, "render": function (data, type, row, meta) {
                return data = Number(meta.row) + Number(1);
            }
        },
        {title: "搜救海区", data: "searchArea"},
        {title: "发生时间", data: "occurredTime"},
        {title: "情况说明", data: "details"},
        {
            title: "操作", data: "id", "orderable": false, // 禁用排序
            "defaultContent": "", "render": function (data, type, row, meta) {
                if (hasDeletePermission) {
                    deleteStr = "<button data-id=" + data + "  type='button' class='btn btn-danger data-delete'>删除</button>&emsp;"
                }
                if (hasUpdatePermission) {
                    updateStr = "<button data-id=" + data + " type='button' class='btn btn-info data-update' data-toggle='modal' data-target='#updateForeignBaseModal'>更新</button>";
                }
                return data = deleteStr + updateStr;
            }
        }
    ];
    let url = 'foreignBase/getAllForeignBase';
    //初始化表格
    dataTableInit(url, columns);
}

//动态绑定
function dynamicBinding() {
    //为所有的delete按钮绑定delete事件
    deleteBind('/foreignBase/deleteForeignBase');
    //为所有的update按钮绑定update事件
    $("#datatable").on('click', ".data-update", function () {
        let data_id = $(this).attr('data-id');
        //获取此行数据
        let row = $("#datatable").DataTable().row($(this).parents('tr')).data();
        //遍历更新表单的所有拥有name的输入框，将行数据填充到这些输入框中，file除外
        $('#toUpdateForeignBase'+" [name]").each(function () {
            if($(this).attr('type')!=="file"){
                let name =  $(this).attr("name");
                $(this).val(row[name]);
            }
        });
        $('#toUpdateForeignBase').parsley().on('form:validate', function () {
        }).on('form:submit', function () {
            let id = data_id;
            let data = new FormData($('#toUpdateForeignBase')[0]);
            data.set('occurredTime', Date($(" #updateOccurredTime ").val()));
            data.set("id", id);
            updateAjax(data, '/foreignBase/updateForeignBase', false, false, "#updateForeignBaseModal");
            return false;
        });
    });
    //为所有的add按钮绑定add事件
    $('#toAddForeignBase').parsley().on('form:validate', function () {
    }).on('form:submit', function () {
        let data = new FormData($('#toAddForeignBase')[0]);
        data.set("occurredTime", Date($(" #addOccurredTime").val()));
        addAjax(data, '/foreignBase/addForeignBase', false, false, "#addForeignBaseModal");
        return false;
    });

    //为add模态框绑定消失事件-清空模态框中内容
    $('#addForeignBaseModal').on('hidden.bs.modal', function () {
        document.getElementById("toAddForeignBase").reset();
    });
    //为update模态框绑定消失事件-清空模态框中内容
    $('#updateForeignBaseModal').on('hidden.bs.modal', function () {
        document.getElementById("toUpdateForeignBase").reset();
    });


    /*
    //为所有的delete按钮绑定delete事件
    deleteBind("#foreignBase-table-body",".data-delete","data-id",'/foreignBase/deleteForeignBase');
    //为所有的update按钮绑定update事件
    <!--String searchArea, Date occurredTime, String details-->
    $("#foreignBase-table-body").on('click', ".data-update", function(){
        let _id=$(this).attr('data-id');
        $("#toUpdateForeignBase").ajaxForm(function () {
            let id = _id;
            let searchArea = $(" #updateSearchArea ").val();
            let occurredTime = new Date($(" #updateOccurredTime ").val());
            let details = $(" #updateDetails ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/foreignBase/updateForeignBase',
                data: { id: id,searchArea:searchArea,occurredTime:occurredTime, details:details},
                success: function (result) {
                    if (result.code==200){
                        window.location.reload();
                    }else {
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });
    //为add按钮绑定add事件
    $("#toAddForeignBase").on('click', function(){
        $("#toAddForeignBase").ajaxForm(function () {
            let searchArea = $(" #addSearchArea ").val();
            let occurredTime = new Date($(" #addOccurredTime ").val());
            let details = $(" #addDetails ").val();
            $(this).ajaxSubmit({
                type: 'POST',
                url: '/foreignBase/addForeignBase',
                data: { searchArea:searchArea,occurredTime:occurredTime, details:details},
                success: function (result) {
                    if(result.code==200){
                        window.location.reload();
                    }else{
                        console.log(result.msg);
                    }
                },
                error: function (result) {
                    return result;
                }
            })
        });
    });*/
}
